package com.ljx.rabbit.controller;

import com.ljx.rabbit.domain.dto.ItemCommentDTO;
import com.ljx.rabbit.domain.dto.OrderDTO;
import com.ljx.rabbit.domain.dto.OrderPageDTO;
import com.ljx.rabbit.domain.po.Order;
import com.ljx.rabbit.domain.vo.OrderPageVo;
import com.ljx.rabbit.domain.vo.OrderSubmitVO;
import com.ljx.rabbit.domain.vo.OrderVo;
import com.ljx.rabbit.result.R;
import com.ljx.rabbit.service.ICartService;
import com.ljx.rabbit.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDateTime;

@Slf4j
@CrossOrigin
@Api(tags = "订单模块")
@RestController
@RequestMapping("/member/order")
@RequiredArgsConstructor
public class OrderController {
    private final ICartService cartService;
    private final IOrderService orderService;

    @ApiOperation(value = "生成订单结算页")
    @GetMapping("/pre")
    public R<OrderSubmitVO> preOrder(@RequestParam(required = false) Long id) {
        OrderSubmitVO orderSubmitVO = cartService.preOrder(id);
        return R.success(orderSubmitVO);
    }

    @ApiOperation(value = "提交订单")
    @PostMapping
    public R<Order> submitOrder(@RequestBody OrderDTO orderDTO) {
        Order order = orderService.submitOrder(orderDTO);
        return R.success(order);
    }

    @ApiOperation(value = "获取订单详情")
    @GetMapping("/{orderId}")
    public R<OrderVo> getOrder(@PathVariable Long orderId) {
        Order order = orderService.getById(orderId);
        OrderVo orderVo = new OrderVo();
        BeanUtils.copyProperties(order, orderVo);
        // 使用 Duration 计算时间差
        Duration duration = Duration.between(LocalDateTime.now(), order.getPayEnd());
        long seconds = duration.getSeconds();
        System.out.println("时间差（秒）: " + seconds);
        orderVo.setCountdown(seconds);
        return R.success(orderVo);
    }

    @ApiOperation(value = "余额支付订单")
    @GetMapping("/pay/{orderId}")
    public R payOrder(@PathVariable Long orderId) {
        orderService.payOrder(orderId);
        return R.success();
    }

    @ApiOperation(value = "查看订单")
    @GetMapping
    public R<OrderPageVo> getOrderPage(@RequestParam Integer orderState,
                                       @RequestParam Integer page,
                                       @RequestParam Integer pageSize) {
        OrderPageDTO params = new OrderPageDTO();
        params.setOrderState(orderState);
        params.setPage(page);
        params.setPageSize(pageSize);
        OrderPageVo orderPage = orderService.getOrderPage(params);
        return R.success(orderPage);
    }

    @ApiOperation(value = "取消订单")
    @PutMapping("/cancel/{orderId}")
    public R cancelOrder(@PathVariable Long orderId) {
        orderService.cancelOrder(orderId);
        return R.success();
    }

    @ApiOperation(value = "删除订单")
    @DeleteMapping("/{orderId}")
    public R deleteOrder(@PathVariable Long orderId) {
        orderService.delById(orderId);
        return R.success();
    }

    @ApiOperation(value = "确认收获->待评价")
    @PutMapping("/confirm/{orderId}")
    public R confirmOrder(@PathVariable Long orderId) {
        orderService.confirmOrder(orderId);
        return R.success();
    }

    @ApiOperation(value = "评价商品")
    @PostMapping("/comment")
    public R commentItem(@RequestBody ItemCommentDTO itemCommentDTO) {
        orderService.commentItem(itemCommentDTO);
        return R.success();
    }
}
